package app.dis.presentacion.beans;

import java.util.List;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import app.dis.accesoadatos.entidades.Cliente;


@ManagedBean
@SessionScoped
public class LoginBean {

	Logger log = Logger.getLogger(this.getClass().getName());

	Cliente personaLogueda;

	String usuario;
	String password;

	public String Login() {

		try {
			ExternalContext cntxt = FacesContext.getCurrentInstance()
					.getExternalContext();
			HttpServletRequest req = (HttpServletRequest) cntxt.getRequest();
			log.info("usuario" + this.getUsuario() + " password" + this.getPassword());

			
			req.login(this.getUsuario(), this.getPassword());

			personaLogueda = Consultar().get(0);

				
			
			
			if (req.isUserInRole("administrador")){
				log.info("ROL ADMIN");
				return "/Sistema/administrador/IngresarPersona.xhtml?faces-redirect=true";
			}
				
			
				

			if (req.isUserInRole("cliente")){
				log.info("ROL CLIENTE");
				return "/Sistema/clientes/MenuCLiente.xhtml?faces-redirect=true";	
			}

			
				

			if(req.isUserInRole("")){
				log.info("Ningun ROL A");
				return "/LoginError.xhtml?faces-redirect=true";
			}
			if(req.isUserInRole(null)){
				log.info("Ningun ROL B");
				return "/LoginError.xhtml?faces-redirect=true";
			}
			

			else{
				log.info("Ningun ROL");
				return "/LoginError.xhtml?faces-redirect=true";
			}
				

		} catch (Exception e) {

			e.printStackTrace();
			log.info("CATCH  ROL"+e.getMessage());

			return "/LoginError.xhtml?faces-redirect=true";
		}

	}
public String logout() {

		

		try {

			ExternalContext cntxt = FacesContext.getCurrentInstance()
					.getExternalContext();
			HttpServletRequest req = (HttpServletRequest) cntxt.getRequest();

			req.logout();

		} catch (Exception e) {
			e.printStackTrace();

		}

		return "/index.xhtml?faces-redirect=true";
	}

	@Inject
	EntityManager em;

	public List<Cliente> Consultar() {

		String sql = "select * from Cliente c,Usuario u ,cliente_usuario cu where c.id=cu.cliente_id  and u.id=cu.usuarios_id and u.nombreusuario like '"
				+ this.getUsuario() + "'";

		return em.createNativeQuery(sql, Cliente.class).getResultList();

	}

	public String nombresCompletos() {

		return personaLogueda.getNombre() + "  " + personaLogueda.getApellido();

	}

	public Cliente getPersonaLogueda() {
		return personaLogueda;
	}

	public void setPersonaLogueda(Cliente personaLogueda) {
		this.personaLogueda = personaLogueda;
	}

	public String getUsuario() {
		return usuario;
	}

	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
